package com.wandousoushu.jiusen.ui.book.read

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.WindowManager
import android.view.animation.Animation
import android.widget.FrameLayout
import android.widget.SeekBar
import androidx.core.view.isVisible
import com.wandousoushu.jiusen.MyApplication
import com.wandousoushu.jiusen.R
import com.wandousoushu.jiusen.booksource.constant.EventBus
import com.wandousoushu.jiusen.booksource.constant.PreferKey
import com.wandousoushu.jiusen.booksource.data.entities.ReadBook
import com.wandousoushu.jiusen.help.AppConfig
import com.wandousoushu.jiusen.help.ReadBookConfig
import com.wandousoushu.jiusen.lib.theme.*
import com.wandousoushu.jiusen.util.*
import kotlinx.android.synthetic.main.view_read_menu.view.*
import org.jetbrains.anko.sdk27.listeners.onClick
import org.jetbrains.anko.sdk27.listeners.onLongClick

class ReadMenu : FrameLayout {
    var cnaShowMenu: Boolean = false
    private var callBack: CallBack? = null
    private lateinit var menuTopIn: Animation
    private lateinit var menuTopOut: Animation
    private lateinit var menuBottomIn: Animation
    private lateinit var menuBottomOut: Animation
    private val bgColor: Int
    private val textColor: Int
    private var bottomBackgroundList: ColorStateList
    private var onMenuOutEnd: (() -> Unit)? = null
    val showBrightnessView get() = context.getPrefBoolean(PreferKey.showBrightnessView, true)

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    init {
        callBack = activity as? CallBack
        bgColor = context.bottomBackground
        textColor = context.getPrimaryTextColor(ColorUtils.isColorLight(bgColor))
        bottomBackgroundList = Selector.colorBuild()
            .setDefaultColor(bgColor)
            .setPressedColor(ColorUtils.darkenColor(bgColor))
            .create()
        inflate(context, R.layout.view_read_menu, this)
        if (AppConfig.isNightTheme) {
            tv_daynight.setText(R.string.day)
            iv_setting.setImageResource(R.drawable.ic_daytime)
        } else {
            tv_daynight.setText(R.string.night)
            iv_setting.setImageResource(R.drawable.ic_brightness)
        }
        initAnimation()
        val brightnessBackground = GradientDrawable()
        brightnessBackground.cornerRadius = 5F.dp
        brightnessBackground.setColor(ColorUtils.adjustAlpha(bgColor, 0.5f))
        ll_brightness.background = brightnessBackground
        ll_bottom_bg.setBackgroundColor(bgColor)
//        fabAutoPage.backgroundTintList = bottomBackgroundList
//        fabAutoPage.setColorFilter(textColor)
//        fabReplaceRule.backgroundTintList = bottomBackgroundList
//        fabReplaceRule.setColorFilter(textColor)
//        fabNightTheme.backgroundTintList = bottomBackgroundList
//        fabNightTheme.setColorFilter(textColor)
        tv_pre.setTextColor(textColor)
        tv_next.setTextColor(textColor)
        iv_catalog.setColorFilter(textColor)
        tv_catalog.setTextColor(textColor)
//        iv_read_aloud.setColorFilter(textColor)
//        tv_read_aloud.setTextColor(textColor)
        iv_font.setColorFilter(textColor)
        tv_font.setTextColor(textColor)
        iv_setting.setColorFilter(textColor)
        tv_daynight.setTextColor(textColor)
        vw_bg.onClick { }
        vwNavigationBar.onClick { }
        seek_brightness.progress = context.getPrefInt("brightness", 100)
        upBrightnessState()
        bindEvent()
    }

    fun upBrightnessState() {
        if (brightnessAuto()) {
            iv_brightness_auto.setColorFilter(context.accentColor)
            seek_brightness.isEnabled = false
        } else {
            iv_brightness_auto.setColorFilter(context.buttonDisabledColor)
            seek_brightness.isEnabled = true
        }
        setScreenBrightness(context.getPrefInt("brightness", 100))
    }

    /**
     * 设置屏幕亮度
     */
    private fun setScreenBrightness(value: Int) {
        var brightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
        if (!brightnessAuto()) {
            brightness = value.toFloat()
            if (brightness < 1f) brightness = 1f
            brightness /= 255f
        }
        val params = activity?.window?.attributes
        params?.screenBrightness = brightness
        activity?.window?.attributes = params
    }

    @SuppressLint("ResourceAsColor")
    fun runMenuIn() {
//        if(AppConfig.isNightTheme){
//            read_title_bar.setBackgroundColor(R.color.md_grey_900)
//        }else{
//            read_title_bar.setBackgroundColor(R.color.colorPrimary)
//        }
        this.visible()
        read_title_bar.visible()
        bottom_menu.visible()
        read_title_bar.startAnimation(menuTopIn)
        bottom_menu.startAnimation(menuBottomIn)
    }

    fun runMenuOut(onMenuOutEnd: (() -> Unit)? = null) {
        this.onMenuOutEnd = onMenuOutEnd
        if (this.isVisible) {
            read_title_bar.startAnimation(menuTopOut)
            bottom_menu.startAnimation(menuBottomOut)
        }
    }

    private fun brightnessAuto(): Boolean {
        return context.getPrefBoolean("brightnessAuto", true) || !showBrightnessView
    }

    @SuppressLint("ResourceAsColor")
    private fun bindEvent() {
        iv_brightness_auto.onClick {
            context.putPrefBoolean("brightnessAuto", !brightnessAuto())
            upBrightnessState()
        }
        //亮度调节
        seek_brightness.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                setScreenBrightness(progress)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                context.putPrefInt("brightness", seek_brightness.progress)
            }

        })

        //阅读进度
        seek_read_page.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean) {

            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                ReadBook.skipToPage(seekBar.progress)
            }
        })

        //自动翻页
//        fabAutoPage.onClick {
//            runMenuOut {
//                callBack?.autoPage()
//            }
//        }

        //替换
//        fabReplaceRule.onClick { callBack?.openReplaceRule() }

        //夜间模式
//        fabNightTheme.onClick {
//            AppConfig.isNightTheme = !AppConfig.isNightTheme
//            MyApplication.instance.applyDayNight()
//            postEvent(EventBus.RECREATE, "")
//        }

        //上一章
        tv_pre.onClick { ReadBook.moveToPrevChapter(upContent = true, toLast = false) }

        //下一章
        tv_next.onClick { ReadBook.moveToNextChapter(true) }

        //目录
        ll_catalog.onClick {
            runMenuOut {
                callBack?.openChapterList()
            }
        }

//        //朗读
//        ll_read_aloud.onClick {
//            runMenuOut {
//                callBack?.onClickReadAloud()
//            }
//        }
//        ll_read_aloud.onLongClick {
//            runMenuOut { callBack?.showReadAloudDialog() }
//            true
//        }
        //界面
        ll_font.onClick {
            runMenuOut {
                callBack?.showReadStyle()
            }
        }

        //夜间
        ll_daynight.onClick {
            AppConfig.isNightTheme = !AppConfig.isNightTheme
            MyApplication.instance.applyDayNight()

            postEvent(EventBus.RECREATE, "")
            postEvent(EventBus.UP_CONFIG, false)
//            runMenuOut {
//                callBack?.showMoreSetting()
//            }
        }
    }

    private fun initAnimation() {
        //显示菜单
        menuTopIn = AnimationUtilsSupport.loadAnimation(context, R.anim.anim_readbook_top_in)
        menuBottomIn = AnimationUtilsSupport.loadAnimation(context, R.anim.anim_readbook_bottom_in)
        menuTopIn.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation) {
                callBack?.upSystemUiVisibility()
                ll_brightness.visible(showBrightnessView)
            }

            override fun onAnimationEnd(animation: Animation) {
                vw_menu_bg.onClick { runMenuOut() }
                vwNavigationBar.layoutParams = vwNavigationBar.layoutParams.apply {
                    height =
                        if (ReadBookConfig.hideNavigationBar && Help.isNavigationBarExist(activity))
                            context.navigationBarHeight
                        else 0
                }
            }

            override fun onAnimationRepeat(animation: Animation) {

            }
        })

        //隐藏菜单
        menuTopOut = AnimationUtilsSupport.loadAnimation(context, R.anim.anim_readbook_top_out)
        menuBottomOut =
            AnimationUtilsSupport.loadAnimation(context, R.anim.anim_readbook_bottom_out)
        menuTopOut.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation) {
                vw_menu_bg.setOnClickListener(null)
            }

            override fun onAnimationEnd(animation: Animation) {
                this@ReadMenu.invisible()
                read_title_bar.invisible()
                bottom_menu.invisible()
                cnaShowMenu = false
                onMenuOutEnd?.invoke()
                callBack?.upSystemUiVisibility()
            }

            override fun onAnimationRepeat(animation: Animation) {

            }
        })
    }

//    fun setAutoPage(autoPage: Boolean) {
//        if (autoPage) {
//            fabAutoPage.setImageResource(R.drawable.ic_auto_page_stop)
//            fabAutoPage.contentDescription = context.getString(R.string.auto_next_page_stop)
//        } else {
//            fabAutoPage.setImageResource(R.drawable.ic_auto_page)
//            fabAutoPage.contentDescription = context.getString(R.string.auto_next_page)
//        }
//        fabAutoPage.setColorFilter(textColor)
//    }

    interface CallBack {
        fun autoPage()
        fun openReplaceRule()
        fun openChapterList()
        fun showReadStyle()
        fun showMoreSetting()
        fun showReadAloudDialog()
        fun upSystemUiVisibility()
        fun onClickReadAloud()
    }

}
